﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Ccit.Utils;
using System.Text;
using System.Collections.Generic;
using Newtonsoft.Json;
using Ccit.OA.MsSystemDoc;

namespace Ccit.OA.SystemDoc
{
    public partial class SystemDoc_search_tree : Ccit.Utils.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this._hidSysDocId.Value = (null != Request.QueryString["SysDocId"]) ? Request.QueryString["SysDocId"].ToString() : "";
                this._hidClassType.Value = (null != Request.QueryString["classType"]) ? Request.QueryString["classType"].ToString() : "";
                this._hidNodeTab.Value = (null != Request.QueryString["NodeTab"]) ? Request.QueryString["NodeTab"].ToString() : "0";
                initializePage();
            }
        }

        /// <summary>
        /// 初始化页面

        /// </summary>
        private void initializePage()
        {
            createTree();
            trvDepAddress.ExpandLevel = 1;
        }
        private void createTree()
        {
            /* c创建根节点 */
            Microsoft.Web.UI.WebControls.TreeNode oRoot = new Microsoft.Web.UI.WebControls.TreeNode();
            ///调用创建根节点方法

            createRootNode(ref oRoot);
            ///添加第一层节点

           
            ///添加节点信息
            //createSubNodes(ref oRoot);
            
            trvDepAddress.Nodes.Add(oRoot);
        }
        /// <summary>
        /// 创建根节点

        /// </summary>
        /// <param name="oRoot"></param>
        private void createRootNode(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            //Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();
            SystemDocBLL Bll = new SystemDocBLL();
            string strAddrXML = Bll.GetCodeRuleMes(_hidClassType.Value);   // get one stair ast type 
            XElement XeAddr = XElement.Parse(strAddrXML);
            string CodeID = "";
            string CodeNAME = "";
            string hasChild = "";

            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                CodeID = eAddr.Element("CodeID").Value;
                CodeNAME = eAddr.Element("CodeNAME").Value;
                hasChild = eAddr.Attribute("HASCHILD").Value;
                if (_hidNodeTab.Value == "0")
                {
                    oRoot.Text = CodeNAME;
                }
                else
                {
                    oRoot.Text = CodeID;
                }
                oRoot.ImageUrl = "../images/toolbar/folder_open.gif";

                oRoot.ID = "0_" + "0" + "_06" + "_" + CodeID;
                oRoot.Type = "root";
                createSubNodes(ref oRoot);
                //oRoot.Nodes.Add(oNode);
            }
            XeAddr = null;
            Bll = null;
        }
        /// <summary>
        /// 节点信息
        /// </summary>
        /// <param name="oRoot"></param>
        private void createSubNodes(ref Microsoft.Web.UI.WebControls.TreeNode oRoot)
        {
            SystemDocBLL Bll = new SystemDocBLL();
            string strAddrXML = Bll.GetSysDocNodes(_hidClassType.Value, _hidNodeTab.Value);   // get one stair ast type 
            Microsoft.Web.UI.WebControls.TreeNode oTreeNode;
            XElement XeAddr = XElement.Parse(strAddrXML);
            string SysDocID = "";
            string SysDocNAME = "";
            string hasChild = "";

            var eAddrArray = from eAddr in XeAddr.Elements("ADDR") select eAddr;

            foreach (var eAddr in eAddrArray)
            {
                SysDocID = eAddr.Element("SysDocID").Value;
                SysDocNAME = eAddr.Element("SysDocNAME").Value;
                hasChild = eAddr.Attribute("HASCHILD").Value;
                oTreeNode = createTreeNode(SysDocNAME, SysDocID, hasChild);
                oRoot.Nodes.Add(oTreeNode);
            }
            XeAddr = null;
            Bll = null;

        }
        /// <summary>
        /// 创建节点信息
        /// </summary>
        /// <param name="addrName">节点名称</param>
        /// <param name="addrId">节点id</param>
        /// <param name="hasChild">子节点id</param>
        /// <param name="state">状态</param>
        /// <returns></returns>
        private Microsoft.Web.UI.WebControls.TreeNode createTreeNode(string SysDocNAME, string SysDocID, string hasChild)
        {
            Microsoft.Web.UI.WebControls.TreeNode oNode = new Microsoft.Web.UI.WebControls.TreeNode();

            oNode.Text = SysDocNAME;
            SystemDocBLL bll = new SystemDocBLL();

            string sShare = bll.GetShare(SysDocID);
            string sParam = "folderID=" + SysDocID + "&classType=" + this._hidClassType.Value + "&Share=" + sShare;
            string encodeParam = Base64Lib.Encode(sParam);
            /*当前用户对该文件夹 无任何操作权限时 */
            if (sShare == "00" || sShare == "01")
            {
                oNode.NavigateUrl = "./SystemDoc_search_list.aspx?" + encodeParam;
                oNode.Target = "iFrmEdit";

            }

            oNode.ImageUrl = "../images/toolbar/folder_open.gif";
            oNode.ID = SysDocID + "_" + "0" + "_" + sShare + "_" + _hidClassType.Value;
            oNode.Type = "root";

            if (sShare != "05")
            {
                if (hasChild == "1")
                {
                    Microsoft.Web.UI.WebControls.TreeNode oSubNode = new Microsoft.Web.UI.WebControls.TreeNode();
                    oNode.Nodes.Add(oSubNode);
                }
            }

            return oNode;
        }
        #region Ajax Methods
        /// <summary>
        /// 新增节点信息get all sub dep addresses
        /// </summary>
        /// <param name="addrId"></param>
        /// <param name="nodeIndex"></param>
        /// <returns></returns>
        [global::System.Web.Services.WebMethod]
        public static string GetSubAddrsByUpAddrId(string addrId, string nodeIndex, string expandSign, string levelStruct, string sClassType, string sNodeTab)
        {

            SystemDocBLL BLL = new SystemDocBLL();
            List<MsSecondClass> lstSec = BLL.GetSysDocNodesXMLmanage(addrId, sClassType, sNodeTab);
            BLL = null;
            string strResult = "GetSubTypesByColumnId" + "_" + JavaScriptConvert.SerializeObject(lstSec) + "_" + nodeIndex + "_" + expandSign + "_" + levelStruct + "_" + addrId;
            return strResult;
        }

        [global::System.Web.Services.WebMethod]
        public static string GetNodeLevelStruct(string dtNodeId)
        {
            StringBuilder sb = new StringBuilder("<RESULT>");
            SystemDocBLL BLL = new SystemDocBLL();
            string levelStruct = BLL.GetNodeLevelStruct(dtNodeId);
            var oResult = new { resultIds = levelStruct };
            string strResult = "GetNodeLevelStruct" + "_" + JavaScriptConvert.SerializeObject(oResult);
            return strResult;
        }
        #endregion
    }
}
